package com.syspeak.modules.spring.web.servlet.mvc.annotation.method.support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.core.Ordered;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.syspeak.modules.spring.web.servlet.mvc.annotation.JsonConfig;

public class JsonLibReturnValueProcessor implements HandlerMethodReturnValueHandler, Ordered {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	private int order = Integer.MAX_VALUE;

	public JsonLibReturnValueProcessor() {
		System.out.println("init..." + this);
		logger.debug("init..." + this);
	}

	@Override
	public boolean supportsReturnType(MethodParameter returnType) {
		return returnType.getMethodAnnotation(JsonConfig.class) != null;
	}

	@Override
	public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer,
			NativeWebRequest webRequest) throws Exception {
		mavContainer.setRequestHandled(true);
	}

	@Override
	public int getOrder() {
		logger.debug(this + " order: " + order);
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}
}
